import Vue from 'vue'
import VueRouter from 'vue-router'
import Admin from '../views/Admin/Admin.vue'
import {
  isToken
} from '../utils/index'

Vue.use(VueRouter)

const routes = [{
  path: '/',
  redirect: '/admin'
},
{
  path: '/login',
  name: '登录',
  component: () => import('../views/Login/index.vue')
},
{
  path: '/admin',
  name: 'Admin',
  meta: {
    needLogin: false
  },
  component: Admin,
  children: [{
    path: '/admin',
    redirect: '/dashBoard',
    meta: {
      isNav: false,
      needLogin: false,
      iconCls: 'el-icon-cold-drink',
      roles: ['superAdmin', 'admin', 'aa']
    }
  },
  {
    path: '/dashBoard',
    name: '档案表',
    meta: {
      isNav: true,
      needLogin: false,
      iconCls: 'el-icon-cold-drink',
      roles: ['superAdmin', 'admin', 'aa']
    },
    component: () => import('../views/DashBoard/DashBoard.vue')
  },
  {
    path: '/itemLists',
    name: '档案室',
    meta: {
      isNav: true,
      needLogin: true,
      iconCls: 'el-icon-menu',
      roles: ['superAdmin', 'admin', 'aa']
    },
    component: () => import('../views/ItemLists/ItemLists.vue')
  },
  {
    path: '/itemAdd',
    name: '添加商品',
    meta: {
      isNav: false,
      needLogin: true,
      roles: ['superAdmin', 'admin', 'aa']
    },
    component: () => import('../views/ItemAdd/ItemAdd.vue')
  },
  {
    path: '/msgLists',
    name: '消息中心',
    meta: {
      isNav: true,
      needLogin: true,
      iconCls: 'el-icon-bicycle',
      roles: ['superAdmin', 'aa']
    },
    component: () => import('../views/MsgLists/index.vue')
  },
  {
    path: '/itemUpdate',
    name: '商品编辑',
    meta: {
      isNav: false,
      needLogin: true,
      roles: ['superAdmin', 'admin', 'aa']
    },
    component: () => import('../views/ItemUpdate/ItemUpdate.vue')
  },
  {
    path: '/setting',
    name: '速归',
    meta: {
      isNav: true,
      needLogin: true,
      iconCls: 'el-icon-setting',
      roles: ['superAdmin', 'aa']
    },
    component: () => import('../views/Setting/index.vue')
  },
  {
    path: '/Access',
    name: '鉴权',
    meta: {
      isNav: false,
      needLogin: true,
      roles: ['superAdmin', 'admin', 'aa']
    },
    component: () => import('../views/Access/index.vue')
  }
  ]
},
{
  path: '*',
  name: '404',
  component: () => import('../views/NotFound/index.vue')
}
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 全局守卫
router.beforeEach((to, from, next) => {
  // 判断是否需要登录
  // console.log('to =====>', to)
  if (to.meta.needLogin === true) {
    // 判断是否已经登录
    // console.log('isToken =====>', isToken())
    if (isToken()) {
      // if (to.meta.roles.includes(JSON.parse(localStorage.getItem('role')))) {
      if (to.meta.roles.includes(localStorage.getItem('role'))) {
        next()
      } else {
        next('/Access')
      }
      // next()
    } else {
      next({
        // 转跳登录页并留下回来的地址
        path: '/login',
        query: {
          from: to.path
        }
      })
    }
  } else {
    next()
  }
})

export default router
